# task_master.py
import queue
import time
from multiprocessing import Queue
from multiprocessing.managers import BaseManager

# 从BaseManager继承的QueueManager:
import facade
from xjlibrary.our_file_dir import BaseDir
from xjlibrary.tools.BaseIp import GetLocalIPByPrefix

curPath = BaseDir.get_file_dir_absolute(__file__)
curPath = BaseDir.get_upper_dir(curPath, -1)
configfile = BaseDir.get_new_path(curPath, "db.ini")


class QueueManager(BaseManager):
    pass


class SendTask(object):
    def __init__(self):
        # 发送任务的队列:
        self.task_queue = Queue(300)
        # 接收结果的队列:
        self.result_queue = Queue()
        # 发送任务的队列:
        self.task_ip_queue = Queue(300)
        # 接收结果的队列:
        self.result_ip_queue = Queue()
        self.register()
        self.ip = GetLocalIPByPrefix("192.168.")
        # 绑定端口5000, 设置验证码'abc':
        self.manager = QueueManager(address=(self.ip, 5002), authkey=b'abc')
        self.task = None
        self.task_ip = None
        self.result_ip = None
        self.result = None

    def register(self):
        # 把两个Queue都注册到网络上, callable参数关联了Queue对象:
        QueueManager.register('get_task_ip_queue', callable=self.return_task_ip_queue)
        QueueManager.register('get_result_ip_queue', callable=self.return_result_ip_queue)
        QueueManager.register('get_task_queue', callable=self.return_task_queue)
        QueueManager.register('get_result_queue', callable=self.return_result_queue)

    def return_task_queue(self):
        return self.task_queue

    def return_result_queue(self):
        return self.result_queue

    def return_task_ip_queue(self):
        return self.task_ip_queue

    def return_result_ip_queue(self):
        return self.result_ip_queue

    def start(self):
        # 启动Queue:
        self.manager.start()

    def set_task_result_obj(self):
        # 获得通过网络访问的Queue对象:
        self.task = self.manager.get_task_queue()
        self.result = self.manager.get_result_queue()
        self.task_ip = self.manager.get_task_ip_queue()
        self.result_ip = self.manager.get_result_ip_queue()

    def set_task(self, n):
        # print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"))
        print(n)
        try:
            self.task.put(n, block=False)
        except:
            pass

    def set_ip_task(self, n):
        # print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"))
        print(n)
        try:
            self.task.put(n, block=False)
        except:
            pass

    def get_result(self):
        # 从result队列读取结果:
        print('Try get results...')
        try:
            for i in range(10):
                r = self.result.get(timeout=10)
                print('Result: %s' % r)
        except queue.Empty:
            print('task queue is empty.')

    def shutdown(self):
        # 关闭:
        self.manager.shutdown()
        print('master exit.')


class SelectTask(object):
    def __init__(self):
        self.logger = facade.get_streamlogger()
        # self.connection = MongoClient(
        #     "mongodb://xujiangrw:vipdatacenter@192.168.30.171:27017",
        #     # 如果“True”且服务器正在运行而没有日志记录，阻塞，直到服务器将所有数据文件同步到磁盘为止。
        #     fsync=False
        # )
        # self.db = self.connection['cnipa']
        self.mysqlutiles = facade.MysqlUtiles(configfile,
                                              "db",
                                             logger=self.logger)

    def selectDB(self):
        sql = "select rawid,app_no,pub_no,app_date,pub_date,cpnum from article where stat=0 limit 1000"
        rows = self.mysqlutiles.SelectFromDB(sql)
        # rows = self.db.pageidjson.find({"stat": 0}).limit(100)
        return rows

    # def selectCookie(self):
    #     rows = self.db.cookies.find({"stat": 1, "usestat": 0}, {"ip", 1}).limit(100)
    #     return rows


if __name__ == "__main__":
    sendtask = SendTask()
    select = SelectTask()
    sendtask.start()
    sendtask.set_task_result_obj()
    while True:
        if sendtask.task.qsize() != 0:
            print("task size is:" + str(sendtask.task.qsize()))
        else:
            rows = select.selectDB()
            if rows:
                for row in rows:
                    sendtask.set_task(row)
        # rows = select.selectCookie()
        # if rows:
        #     for row in rows:
        #         sendtask.set_ip_task(row["ip"])
        time.sleep(10)

